<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/2/4
 * Time: 9:55
 */

namespace Api\Controller;
use Api\Model\MsglogModel;
use Think\Controller;


class IndexController extends MapiBaseController {

    protected $promotersModel;
    protected $msgLogModel;
    protected $promotionRegistrationModel;
    public function _initialize(){
        parent::_initialize();
        $this->promotersModel = D("promoters");
        $this->msgLogModel = D("msglog");
        $this->promotionRegistrationModel = D("promotion_registration");
    }
    public function index(){
        $data = $this->data;
        $this->validate('phone_mob',$data['phone_mob'],'',3);
        $this->validate('msg_code',$data['msg_code'],'',1);
        $code = -1;
        $msg = '当前您的手机号无法申请注册链接';
        $postData = array();
        $promotersInfo = $this->promotersModel->where(array("phone_mob"=>$data['phone_mob']))->find();
        if(!empty($promotersInfo)){
            $msg = '当前手机号推广功能已被禁用，请及时联系客服！';
            if($promotersInfo['status'] == 1){
                $msg = '请填写验证码';
                if($data['msg_code']){
//                    $msg = '验证码错误';
//                    if($data['msg_code'] == $_SESSION['MobileConfirmCode']){
                        //进行发短信操作！
                        $time = time() + 24*60*60;
                        $arr = array(
                            'phone'  =>  $data['phone_mob'],
                            'end_time' => $time,
                            'status' =>1
                        );
                        $info = $this->promotionRegistrationModel->add($arr);
                        $msg = '添加失败！';
                        if($info){
                            $value = set_encrypt($info);
                            $codeScrit = md5($data['phone_mob'].'CMPersonPromote'.$value);
                            $websiteURL = C('SITE_URL').'link.php?code='.$codeScrit.'&value='.$value;
                            $key = $codeScrit;
                            $redis = new \Redis();
                            $redis->connect('127.0.0.1', 6379);
                            $redis->set($key,$value);
                            $redis->expire($key,$time);
                            setcookie("userInfo", $promotersInfo,'60*60*24*15');
                            $code = 1;
                            $msg = '申请成功';
                            $postData['url'] = $websiteURL;
//                    }
                        }
                    }
//                }
            }
        }
        $this->ApiReturn($code,$msg,$postData);
    }

    //发送验证码
    public function promotersCode(){
        $data = $this->data;
        $this->validate('phone_mob',$data['phone_mob'],'',3);
        $code = -1;
        $msg = '当前您的手机号无法申请注册链接';
        $postData = array();
        $promotersInfo = $this->promotersModel->where(array("phone_mob"=>$data['phone_mob']))->find();
        if(!empty($promotersInfo)){
            $msg = '当前手机号推广功能已被禁用，请及时联系客服！';
            if($promotersInfo['status'] == 1){
                $msgLogModel = new MsglogModel();
                $info = $msgLogModel->send_msg_system('promoters',$data['phone_mob']);
                $code = 1;
                $msg = '发送成功';
            }
        }
        $this->ApiReturn($code,$msg,$postData);
    }
    //拼接xml
    private function writeXML($phone,$text){
        $data = '<?xml version="1.0" encoding="utf-8" ?>
                    <Body>
                        <user>'.C('MSG_USER').'</user>
                        <password>'.C('MSG_PASSWORD').'</password>
                        <version>'.C('MSG_VERSION').'</version>
                        <submit>
                            <usermsgid>w'.$phone.'</usermsgid>
                            <desttermid>'.$phone.'</desttermid>
                            <srctermid></srctermid>
                            <msgcontent>'.$text.'</msgcontent>
                            <signid>'.C('MSG_SIGNID').'</signid>
                            <desttype>'.C('MSG_DESTTYPE').'</desttype>
                            <needreply>'.C('MSG_NEEDREPLY').'</needreply>
                        </submit>
                    </Body>';
        return $data;
    }



}